/**
 * @Description MyPointController.java
 * @author xu_cc [18724000100@163.com]
 * @date 创建时间：2017年12月4日 下午7:34:10
 */
package com.solution.pc.my.controller;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.time.DateFormatUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.solution.admin.point.constants.PointConstants;
import com.solution.admin.point.constants.PointConstants.PointOperateType;
import com.solution.admin.point.entity.PointEntity;
import com.solution.admin.point.entity.PointLogEntity;
import com.solution.admin.point.entity.SignPointSettingsEntity;
import com.solution.admin.point.service.PointLogService;
import com.solution.admin.point.service.PointService;
import com.solution.admin.point.service.SignPointSettingsService;
import com.solution.admin.settings.entity.KeyMapEntity;
import com.solution.admin.settings.service.KeyMapService;
import com.solution.common.controller.BaseController;
import com.solution.common.page.PageBean;
import com.solution.common.page.PageParam;

/**
 * 我的积分
 * 
 * @author xu_cc
 * @date 创建时间：2017年12月4日 下午7:34:28
 */
@RequestMapping("/pc/my")
@Controller("myPointController")
public class MyPointController extends BaseController {

	@Autowired
	PointService pointService;

	@Autowired
	PointLogService pointLogService;
	
	@Autowired
	SignPointSettingsService signPointSettingsService;
	
	@Autowired
	KeyMapService keyMapService;

	/**
	 * 积分中心
	 * 
	 * @param request
	 * @param model
	 * @return String
	 * @author xu_cc
	 * @throws Exception
	 *             exception
	 * @Date 创建时间：2017年12月4日 上午11:31:31
	 */
	@RequestMapping("/point")
	public String toMyPointPage(HttpServletRequest request, Model model) {
		try {
			PageParam pageParam = super.getPageParam(request, 10);
			Map<String, Object> paramMap = new HashMap<String, Object>();
			Long customerId = getCustomerId();
			// 查询客户积分余额
			if (null != customerId) {
				PointEntity point = pointService.getPointByCustomerId(customerId);
				paramMap.put("customerId", customerId);
				paramMap.put("terminalType", PointConstants.FRONTEND_VIEW_POINT_LOG);
				PageBean pageBean = pointLogService.listPointLogPage(pageParam, paramMap);
				model.addAttribute("point", point.getPoint());
				model.addAttribute("pointList", pageBean.getRecordList());
				model.addAttribute("pageBean", pageBean);
			} else {
				model.addAttribute("point", 0);
			}
			
			// 今天生效的签到积分规则
			String pattern = "yyyy-MM-dd";
			String currentDateStr = DateFormatUtils.format(new Date(), pattern);
			// 查看今天是否有签到积分设置信息
			Map<String, Object> paramSign = new HashMap<String, Object>();
			paramSign.put("startTime", currentDateStr);
			paramSign.put("endTime", currentDateStr);
			// 查看是否有时间冲突的记录,查询签到获取积分数量
			BigDecimal signPoint = BigDecimal.ZERO;
			List<SignPointSettingsEntity> signPointSettingsList = signPointSettingsService
					.listSignPointSettings(paramSign);
			if (null != signPointSettingsList&& signPointSettingsList.size() > 0) {
				// 获取设置的积分
				signPoint = signPointSettingsList.get(0).getSignPoint();
			}
			request.setAttribute("signPoint", signPoint);
			// 今天是否已经签到
			Map<String, Object> param = new HashMap<String, Object>();
			param.put("customerId", customerId);
			param.put("operateType", PointOperateType.OPT_SIGN.getCode());
			param.put("today", 1);// today只要有值就行，不限制
			List<PointLogEntity> signLogList = pointLogService.listPointLog(param);
			if (null != signLogList && signLogList.size() > 0) {
				request.setAttribute("isSigned", 1);
			} else {
				request.setAttribute("isSigned", 0);
			}
			//签到控制,是否允许签到 1 允许,其他 禁止
			List<KeyMapEntity> signInControlList = keyMapService.listKeyMap("signInControl");
			if (null != signInControlList && signInControlList.size() != 0) {
				KeyMapEntity signInControl = signInControlList.get(0);
				if ("1".equals(signInControl.getKeyValue())) {
					model.addAttribute("signInControl", signInControl.getKeyValue());
				} else {
					model.addAttribute("signInControl", "0");
				}
			}
			
			model.addAttribute("page", "point");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "pc/my/point/my_point";
	}

}
